<?php

namespace app\controller;

use support\bootstrap\Redis;
use support\Db;
use support\Request;

class Blog
{
    /**
     * 首页
     * @param Request $request
     * @return string|\support\Response
     */
    public function index(Request $request)
    {
        try {
            $keyword = $request->get('keyword', null);
            $allArticle = $this->allArticle(); // 所有文章
            if ($keyword) $allArticle = json_decode(json_encode($this->search($keyword)),true);
            $total_items = count($allArticle); // 总文章数
            $limit = 10; // 每页显示N条
            $items_perPage = ceil($total_items / $limit); // 按10条分页能分几页
            $current_page = (int)$request->get('page', 1);
            $result = [];
            for ($i = 0; $i < count($allArticle); $i++) $result[] = array_slice($allArticle, $i * $limit, $limit);
            return view('blog/index', [
                'recentPublish' => $this->recentPublish(),
                'allArticle' => array_filter($result)[$current_page - 1],
                'items_perPage' => $items_perPage,
                'current_page' => $current_page
            ]);
        } catch (\Exception $e) {
            return view('blog/error');
        }
    }

    /**
     * 文章归档
     * @param Request $request
     * @return string|\support\Response
     */
    public function archives(Request $request)
    {
        return view('blog/archives', [
            'recentPublish' => $this->recentPublish(),
            'allArticle' => $this->allArticle()
        ]);
    }

    /**
     * 博客友链
     * @param Request $request
     * @return string|\support\Response
     */
    public function links(Request $request)
    {
        return view('blog/links', ['recentPublish' => $this->recentPublish()]);
    }

    /**
     * 文章详情页
     * @param Request $request
     * @param $id
     * @return string|\support\Response
     */
    public function details(Request $request, $id)
    {
        $posts = Db::table('posts')->where('id', $id)->get();
        return view('blog/details', ['posts' => $posts, 'recentPublish' => $this->recentPublish()]);
    }

    /**
     * 最近发布
     * @return \Illuminate\Support\Collection
     */
    public function recentPublish()
    {
        if (Redis::get('recentPublish')) {
            return json_decode(Redis::get('recentPublish'), true);
        } else {
            $result = DB::table('posts')->select('id', 'title')->orderByDesc('id')->limit(7)->get();;
            Redis::set('recentPublish', json_encode($result, true));
            return json_decode(json_encode($result),true);
        }
    }

    /**
     * 全部文章
     * @return \Illuminate\Support\Collection|mixed
     */
    public function allArticle()
    {
        if (Redis::get('allArticle')) {
            return json_decode(Redis::get('allArticle'), true);
        } else {
            $result = DB::table('posts')->orderByDesc('id')->get();
            Redis::set('allArticle', json_encode($result, true));
            return json_decode(json_encode($result),true);
        }
    }

    public function search($keyword)
    {
        return Db::table('posts')
            ->where('content', 'like', '%' . $keyword . '%')
            ->orWhere('title', 'like', '%' . $keyword . '%')
            ->orderByDesc('id')
            ->get();
    }

}
